<?php
/**
 * <https://y.st./>
 * Copyright © 2016-2017 Alex Yst <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org./licenses/>.
**/

/**
 * Closures are for writing functions that aren't fit to be reused in
 * other projects. If a function isn't fit to be used in other
 * projects, it clearly hasn't been generalized enough. As such, the
 * code in this file really should be generalized more. I'll rewrite
 * this code as I figure out how to better generalize it.
**/

$¢_build_page = function($filename, $xhtml) {
	global $URI_path_minus_slash;
	global $entry;
	global $a;
	global $pgp;
	global $should_exist;
	global $coursework_nav;

	global $interval;
	global $latest_entry;

	if(substr('/'.$filename, -12) == '/index.xhtml'):
		$URI_path_minus_slash = substr($filename, 0, -11);
	else:
		$URI_path_minus_slash = $filename;
	endif;

	if(substr($filename, 0, 3) == 'en/'):
		$nav_en = true;
	endif;

	if($filename != 'en/coursework/index.xhtml' and substr($filename, 0, 14) == 'en/coursework/'):
		$nav_en_coursework = true;
// It's less work to maintain this if we have the script dynamically
// decide what the course code must be.
		$course_path_parts = explode('/', $filename);
// If there's a an element number three, element two is the course
// code. Otherwise, we have no course code at all because we're not
// dealing with a course subdirectory.
		if(isset($course_path_parts[3])):
			$course_code_nav = $course_path_parts[2];
		endif;
	endif;

	if($filename != 'en/opinion/index.xhtml' and substr($filename, 0, 11) == 'en/opinion/'):
		$nav_en_opinion = true;
	endif;

	if(substr($filename, 0, 10) == 'en/weblog/'):
		$current_entry = substr($URI_path_minus_slash, 10);
		$current_entry = explode('/', $current_entry);
		$nav_en_weblog = true;
		switch(count($current_entry)):
// Main weblog index:
			case 1:
			break;
// Weblog year index:
			case 2:
				$nav_en_weblog_year = true;
				$datetime = new DateTimeImmutable("$current_entry[0]-01-01");
				$previous = $datetime->sub($interval['year'])->format('Y');
				$next     = $datetime->add($interval['year'])->format('Y');
			break;
			case 3:
// Weblog month index:
				if($current_entry[2] == ''):
					$nav_en_weblog_month = true;
					$current_entry[1] = explode('-', $current_entry[1])[0];
					$datetime = new DateTimeImmutable("$current_entry[0]-$current_entry[1]-01");
					$previous = $datetime->sub($interval['month'])->format('Y/m-F');
					$next     = $datetime->add($interval['month'])->format('Y/m-F');
// Weblog day entry:
				else:
					$nav_en_weblog_day = true;
					$current_entry[1] = explode('-', $current_entry[1])[0];
					$current_entry[2] = explode('.', $current_entry[2])[0];
					$datetime = new DateTimeImmutable("$current_entry[0]-$current_entry[1]-$current_entry[2]");
					$previous = $datetime->sub($interval['day'])->format('Y/m-F/d');
					$next     = $datetime->add($interval['day'])->format('Y/m-F/d');
					$xhtml['subtitle'] = $entry($current_entry[0], $current_entry[1], $current_entry[2]);
				endif;
			break;
			default:
				throw new Exception("The weblog section of the source directory tree has an anomaly. File path \"$filename\" detected.");
		endswitch;
// Automatic copyright dating of journal entries:
		if(empty($xhtml['copyright year']) and !empty($current_entry[0])):
			$xhtml['copyright year'] = $current_entry[0];
		endif;
	endif;

	ob_start();
?>
<?xml version="1.0" encoding="utf-8"?>
<!--
                                                                                     
 h       t     t                ::       /     /                     t             / 
 h       t     t                ::      //    //                     t            // 
 h     ttttt ttttt ppppp sssss         //    //  y   y       sssss ttttt         //  
 hhhh    t     t   p   p s            //    //   y   y       s       t          //   
 h  hh   t     t   ppppp sssss       //    //    yyyyy       sssss   t         //    
 h   h   t     t   p         s  ::   /     /         y  ..       s   t    ..   /     
 h   h   t     t   p     sssss  ::   /     /     yyyyy  ..   sssss   t    ..   /     
                                                                                     
	<https://y.st./>
	Copyright © <?php echo $xhtml['copyright year']; ?> Alex Yst <mailto:copyright@y.st>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <https://www.gnu.org./licenses/>.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
<?php
	echo <<<END
		<base href="https://y.st./$URI_path_minus_slash" />

END;
?>
		<title><?php echo strip_tags(str_replace(array(
	'<q>',
	'</q>',
), '&quot;', $xhtml['title']))." &lt;https://y.st./$URI_path_minus_slash&gt;"; ?></title>
		<link rel="icon" type="image/png" href="/link/CC_BY-SA_4.0/y.st./icon.png" />
<?php
	if($filename == 'ErrorDocument/400.xhtml'):
?>
		<style type="text/css">
<?php
		echo htmlspecialchars(file_get_contents(__DIR__.'/../static/link/basic.css'), ENT_XML1|ENT_QUOTES),
			htmlspecialchars(file_get_contents(__DIR__.'/../static/link/site-specific.css'), ENT_XML1|ENT_QUOTES);
?>
		</style>
<?php
	else:
?>
		<link rel="stylesheet" type="text/css" href="/link/basic.css" />
		<link rel="stylesheet" type="text/css" href="/link/site-specific.css" />
		<script type="text/javascript" src="/script/javascript.js" />
<?php
	endif;
?>
		<meta name="viewport" content="width=device-width" />
	</head>
	<body>
<?php // Begin navigational header
	if(isset($nav_en)):
?>
		<nav>
			<p>
				<a href="/en/">Home</a> |
				<a href="/en/a/about.xhtml">About</a> |
				<a href="/en/a/contact.xhtml">Contact</a> |
				<a href="/a/canary.txt">Canary</a> |
				<a href="/en/URI_research/"><?php echo $a['URI']; ?> research</a> |
				<a href="/en/opinion/">Opinions</a> |
				<a href="/en/coursework/">Coursework</a> |
				<a href="/en/law/">Law</a> |
				<a href="/en/a/links.xhtml">Links</a> |
<?php
		echo <<<END
				<a href="/$filename.asc">{this page}.asc</a>

END;
?>
			</p>
			<hr/>
<?php
	if(isset($nav_en_weblog)):
?>
			<p>
				Weblog index:
				<a href="/en/weblog/"><?php echo $a['ASCII']; ?> calendars</a> |
				<a href="/en/weblog/index_ol_ascending.xhtml">Ascending list</a> |
				<a href="/en/weblog/index_ol_descending.xhtml">Descending list</a>
			</p>
			<hr/>
<?php
	endif;
	if(isset($nav_en_weblog_year)):
?>
			<p>
				Jump to year:
<?php
		if("/$filename" != '/en/weblog/2015/index.xhtml'):
			echo <<<END
				<a href="/en/weblog/2015/">&lt;&lt;First</a>

END;
		endif;
		if(is_dir(__DIR__."/pages/en/weblog/$previous/")):
			echo <<<END
				<a rel="prev" href="/en/weblog/$previous/">&lt;Previous</a>

END;
		endif;
		if(is_dir(__DIR__."/pages/en/weblog/$next/")):
			echo <<<END
				<a rel="next" href="/en/weblog/$next/">Next&gt;</a>

END;
		endif;
		if("/$filename" != "$latest_entry[year]index.xhtml"):
			echo <<<END
				<a href="/en/weblog/latest_year.xhtml">Latest&gt;&gt;</a>

END;
		endif;
?>
			</p>
			<hr/>
<?php
	endif;
	if(isset($nav_en_weblog_month)): ?>
			<p>
				Jump to month:
<?php
		if("/$filename" != '/en/weblog/2015/03-March/index.xhtml'):
			echo <<<END
				<a href="/en/weblog/2015/03-March/">&lt;&lt;First</a>

END;
		endif;
		if(is_dir(__DIR__."/pages/en/weblog/$previous/")):
			echo <<<END
				<a rel="prev" href="/en/weblog/$previous/">&lt;Previous</a>

END;
		endif;
		if(is_dir(__DIR__."/pages/en/weblog/$next/")):
			echo <<<END
				<a rel="next" href="/en/weblog/$next/">Next&gt;</a>

END;
		endif;
		if("/$filename" != "$latest_entry[month]index.xhtml"):
			echo <<<END
				<a href="/en/weblog/latest_month.xhtml">Latest&gt;&gt;</a>

END;
		endif;
?>
			</p>
			<hr/>
<?php
	endif;
	if(isset($nav_en_weblog_day)):
?>
			<p>
				Jump to entry:
<?php
		if("/$filename" != '/en/weblog/2015/03-March/07.xhtml'):
			echo <<<END
				<a href="/en/weblog/2015/03-March/07.xhtml">&lt;&lt;First</a>

END;
		endif;
		if(file_exists(__DIR__."/pages/en/weblog/$previous.xhtml")):
			echo <<<END
				<a rel="prev" href="/en/weblog/$previous.xhtml">&lt;Previous</a>

END;
		endif;
		if(file_exists(__DIR__."/pages/en/weblog/$next.xhtml")):
			echo <<<END
				<a rel="next" href="/en/weblog/$next.xhtml">Next&gt;</a>

END;
		endif;
		if("/$filename" != $latest_entry['day']):
			echo <<<END
				<a href="/en/weblog/latest.xhtml">Latest&gt;&gt;</a>

END;
		endif;
?>
			</p>
			<hr/>
<?php
	endif;
	if(isset($nav_en_coursework)):
?>
			<p>
				<a href="/en/coursework/BUS1101/" title="Principles of Business Management">BUS 1101</a> |
				<span class="hyperlink_unavailable" title="Basic Accounting">BUS 1102</span> |
				<span class="hyperlink_unavailable" title="Principles of Marketing">BUS 2201</span> |
				<span class="hyperlink_unavailable" title="Multinational Management">BUS 2207</span> |
				<span class="hyperlink_unavailable" title="Business and Society">BUS 3306</span> |
				<a href="/en/coursework/CS1101/" title="Programming Fundamentals">CS 1101</a> |
				<a href="/en/coursework/CS1102/" title="Programming 1">CS 1102</a> |
				<a href="/en/coursework/CS1103/" title="Programming 2">CS 1103</a> |
				<span class="hyperlink_unavailable" title="Computer Systems">CS 1104</span> |
				<a href="/en/coursework/CS2203/" title="Databases 1">CS 2203</a> |
				<span class="hyperlink_unavailable" title="Communications and Networking">CS 2204</span> |
				<a href="/en/coursework/CS2205/" title="Web Programming 1">CS 2205</a> |
				<a href="/en/coursework/CS2301/" title="Operating Systems 1">CS 2301</a> |
				<span class="hyperlink_unavailable" title="Software Engineering 1">CS 2401</span> |
				<span class="hyperlink_unavailable" title="Data Structures">CS 3303</span> |
				<span class="hyperlink_unavailable" title="Analysis of Algorithms">CS 3304</span> |
				<span class="hyperlink_unavailable" title="Web Programming 2">CS 3305</span> |
				<span class="hyperlink_unavailable" title="Databases 2">CS 3306</span> |
				<span class="hyperlink_unavailable" title="Operating Systems 2">CS 3307</span> |
				<span class="hyperlink_unavailable" title="Information Retrieval">CS 3308</span> |
				<span class="hyperlink_unavailable" title="Comparative Programming Languages">CS 4402</span> |
				<span class="hyperlink_unavailable" title="Software Engineering 2">CS 4403</span> |
				<span class="hyperlink_unavailable" title="Advanced Networking and Data Security">CS 4404</span> |
				<span class="hyperlink_unavailable" title="Mobile Applications">CS 4405</span> |
				<span class="hyperlink_unavailable" title="Computer Graphics">CS 4406</span> |
				<span class="hyperlink_unavailable" title="Data Mining and Machine Learning">CS 4407</span> |
				<span class="hyperlink_unavailable" title="Artificial Intelligence">CS 4408</span> |
				<span class="hyperlink_unavailable" title="English Composition 2">ENGL 1102</span> |
				<span class="hyperlink_unavailable" title="World Literature">ENGL 1405</span> |
				<span class="hyperlink_unavailable" title="Introduction to Environmental Science">ENVS 1301</span> |
				<a href="/en/coursework/HIST1421/" title="Greek and Roman Civilization">HIST 1421</a> |
				<span class="hyperlink_unavailable" title="College Algebra">MATH 1201</span> |
				<span class="hyperlink_unavailable" title="Calculus">MATH 1211</span> |
				<span class="hyperlink_unavailable" title="Introduction to Statistics">MATH 1280</span> |
				<span class="hyperlink_unavailable" title="Discrete Mathematics">MATH 1302</span> |
				<span class="hyperlink_unavailable" title="Introduction to Philosophy">PHIL 1402</span> |
				<a href="/en/coursework/PHIL1404/" title="Ethics and Social Responsibility">PHIL 1404</a> |
				<a href="/en/coursework/POLS1503/" title="Globalization">POLS 1503</a> |
				<span class="hyperlink_unavailable" title="Introduction to Psychology">PSYC 1504</span> |
				<a href="/en/coursework/UNIV1001/" title="Online Education Strategies">UNIV 1001</a>
			</p>
			<hr/>
<?php
		if(isset($course_code_nav)):
			echo "\t\t\t<p>\n\t\t\t\t{$coursework_nav[$course_code_nav]}\n\t\t\t</p>\n\t\t\t<hr/>\n";
		endif;
	endif;
	if(isset($nav_en_opinion)):
?>
			<p>
				<a href="/en/opinion/XML.xhtml"><?php echo $a['XML']; ?></a> |
				<a href="/en/opinion/code_indentation.xhtml">Code indentation</a> |
				<a href="/en/opinion/free_culture.xhtml">Free culture</a> |
				<a href="/en/opinion/free_software.xhtml">Free software</a> |
				<a href="/en/opinion/holidays.xhtml">Holidays</a> |
				<a href="/en/opinion/misuse_of_words.xhtml">Misuse of words</a>
			</p>
			<hr/>
<?php
	endif;
?>
		</nav>
<?php
	endif;
// End navigational header
	if(isset($xhtml['subtitle'])):
?>
		<header>
<?php
	endif;
?>
			<h1><?php echo $xhtml['title']; ?></h1>
<?php
	if(isset($xhtml['subtitle'])):
?>
			<p><?php echo $xhtml['subtitle']; ?></p>
		</header>
<?php
	endif;
	echo $xhtml['body'];
?>

		<hr/>
		<p>
			Copyright © <?php echo $xhtml['copyright year']; ?> Alex Yst;
			You may modify and/or redistribute this document under the terms of the <a rel="license" href="/license/gpl-3.0-standalone.xhtml"><?php echo $a['GNU'], ' ', $a['GPLv3+']; ?></a>.
			If for some reason you would prefer to modify and/or distribute this document under other free copyleft terms, please ask me via email.
			My address is in the source comments near the top of this document.
			This license also applies to embedded content such as images.
			For more information on that, see <a href="/en/a/licensing.xhtml">licensing</a>.
		</p>
		<p>
			<?php echo $a['W3C']; ?> standards are important.
			This document conforms to the <?php
	$checkpath = urlencode($URI_path_minus_slash);
	echo '<a href="https://validator.w3.org./nu/?doc=https%3A%2F%2Fy.st.%2F', $checkpath, '">', $a['XHTML'], ' 5.1</a>';
?> specification and uses style sheets that conform to the <?php
	echo '<a href="http://jigsaw.w3.org./css-validator/validator?uri=https%3A%2F%2Fy.st.%2F', $checkpath, '">', $a['CSS'], '3</a>';
?> specification.
		</p>
	</body>
</html>

<?php

	$should_exist[$filename] = true;
	$should_exist["$filename.asc"] = true;
	$dirname = dirname(__DIR__."/../compiled/$filename");
	if(!is_dir($dirname)):
		mkdir($dirname, 0777, true);
	endif;
	\st\y\update_file(__DIR__."/../compiled/$filename", ob_get_clean(), $pgp);
};
